<template>
  <el-dialog
    :title="info.title"
    :visible="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <strong>请输入类目名称:</strong>
    <el-input v-model="input" placeholder="请输入内容"></el-input>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submitHandle">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["info", "type"],
  data() {
    return {
      dialogVisible: false,
      input: "",
    };
  },
  methods: {
    submitHandle() {
      if (this.type == 1) {
        console.log("新增", this.input);
        this.insertItemCategory({ cid: this.info.cid, name: this.input });
      } else if(this.type==2) {
        console.log("修改", this.input);
        this.updateCategory({ id: this.info.id, name: this.input });
      }else{
        console.log("新增一级", this.input);
        this.insertCategory({name:this.input})
      }
    },

    handleClose(done) {
      this.dialogVisible = false;
    },

    async insertCategory(params){
        let res = await this.$api.insertCategory(params);
      if (res.data.status === 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.dialogVisible = false;
        //更新父组件视图
        //即调用父组件的查询分类方法
        //this.$emit('updateViews')
        this.$parent.itemCategory();
      }
    },
    //新增子类目
    async insertItemCategory(params) {
      let res = await this.$api.insertItemCategory(params);
      if (res.data.status === 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });
        this.dialogVisible = false;
        //更新父组件视图
        //即调用父组件的查询分类方法
        //this.$emit('updateViews')
        this.$parent.itemCategory();
      }
    },
    //修改类目{id name}
    async updateCategory(params) {
      let res = await this.$api.updateCategory(params);
      if (res.data.status === 200) {
        this.$message({
          message: "修改成功",
          type: "success",
        });
        this.dialogVisible = false;
        //更新父组件视图
        //即调用父组件的查询分类方法
        //this.$emit('updateViews')
        this.$parent.itemCategory();
      }
    },
  },
};
</script>

<style>
</style>